﻿using MQTTnet.Protocol;

namespace RuoVea.Iot.MqttClient.Server;

/// <summary>
/// 
/// </summary>
public class MqttOptions
{
    /// <summary>
    /// 地址
    /// </summary>
    public string Server { get; set; }
    /// <summary>
    /// 端口
    /// </summary>
    public int? Port { get; set; }

    /// <summary>
    /// 用户名
    /// </summary>
    public string UserName { get; set; }
    /// <summary>
    /// 密码
    /// </summary>
    public string Password { get; set; }

    /// <summary>
    /// 
    /// </summary>
    private string _clientId;
    /// <summary>
    /// ClientId
    /// </summary>
    public string ClientId
    {
        get
        {
            // 如果已设置 _clientId，直接返回
            if (!string.IsNullOrWhiteSpace(_clientId))
                return _clientId;
            return $"{ClientIdPrefix}{ExIdGen.IdGenerator.Id}";
        }
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                _clientId = $"{ClientIdPrefix}{ExIdGen.IdGenerator.Id}";
            else
                _clientId = value;
        }
    }

    /// <summary>
    /// ClientIdPrefix
    /// </summary>
    public string ClientIdPrefix { get; set; } = "RuoVeaClient:V";

    /// <summary>
    /// 默认主题
    /// </summary>
    public string TopicPrefix { get; set; } = "app/ruovea";
    /// <summary>
    /// 心跳配置 秒
    /// </summary>
    public int HeartbeatInterval { get; set; } = 60;
    /// <summary>
    /// 最大限制/字节
    /// </summary>
    public int MaxPayloadSize { get; set; } = 256;

    /// <summary>
    /// 
    /// </summary>
    private string _defaultPublishTopic;
    /// <summary>
    /// 默认发布主题配置
    /// </summary>
    public string DefaultPublishTopic
    {
        get => string.IsNullOrWhiteSpace(_defaultPublishTopic) ? $"{TopicPrefix}/publish" : $"{TopicPrefix}/{_defaultPublishTopic}";
        set => _defaultPublishTopic = value;
    }
    /// <summary>
    /// 
    /// </summary>
    private string _defaultSubscribeTopic;
    /// 默认订阅主题配置
    /// </summary>
    public string DefaultSubscribeTopics
    {
        get => _defaultSubscribeTopic ?? $"{TopicPrefix}/subscribe";
        set => _defaultSubscribeTopic = value;
    }

    /// <summary>
    /// 默认主题的 QoS 级别
    /// </summary>
    public MqttQualityOfServiceLevel DefaultQoS { get; set; } = MqttQualityOfServiceLevel.AtLeastOnce;
}
